package com.zsy.service.impl;

import com.zsy.entity.Project;
import com.zsy.entity.User;
import com.zsy.mapper.IProjectMapper;
import com.zsy.service.IProjectService;
import com.zsy.utils.Result;
//import com.zsy.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class ProjectServiceImpl implements IProjectService {

    @Autowired
    private IProjectMapper ipm;
    // 查询所有项目
    @Override
    public Result getAllProject(Project project) {

//        Map<String, Object> map = ThreadLocalUtil.get();
//        String de = (String) map.get("department");
//        String username = (String) map.get("username");
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        User principal = (User) authentication.getPrincipal();
        List<Project> project1 = ipm.getAllProject();
        List<Object> projectAll = new ArrayList<>();
        projectAll.add(project1);
        projectAll.add(principal.getUsername());
        System.out.println(projectAll);
        if (principal.getDepartment().equals("研发部") || principal.getDepartment().equals("管理员")) {
            return Result.ok(projectAll);
        }else {
            return Result.error("你没有权限");
        }

    }
    // 根据条件查询项目
    @Override
    public Result getProject(Project project) {
        System.out.println("新的："+project);
        List<Project> project1 = ipm.getProject(project);
        if (project1 != null && !project1.isEmpty() ) {
            return Result.ok(project1);
        }
        return Result.error("请重新输入...");
    }
    // 添加项目
    @Override
    public Result addProject(Project project) {
        int i = ipm.addProject(project);
        if (i > 0) {
            return Result.ok("添加成功");
        }
        return Result.error("添加失败");
    }
    // 删除项目
    @Override
    public Result deleteProject(Integer projectId) {
        int i = ipm.deleteProject(projectId);
        if (i > 0) {
            return Result.ok("删除成功");
        }
        return Result.error("删除失败");
    }
    // 修改项目
    @Override
    public Result updateProject(Project project) {
        int i = ipm.updateProject(project);
        if (i > 0) {
            return Result.ok("修改成功");
        }
        return Result.error("修改失败");
    }
    // 批量修改项目状态
    @Override
    public Result updateOrdersStatusToZero(Integer[] projectIds) {
        int i = ipm.updateOrdersStatusToZero(projectIds);
        if (i > 0) {
            return Result.ok("修改成功");
        }
        return Result.error("修改失败");
    }


}